<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Tag;

class TagController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_shop' => 'active open',
            '_tag' => 'active',
            'tags' => Tag::all_tags()
        ]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.shop.tag.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.tag.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:tags|max:255',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        Tag::create($request->all());
        Tag::clear();
        return redirect(route('shop.tag.index'))->with('新增商品分类成功~');

    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $tag = Tag::find($id);
        return view('admin.shop.tag.edit', compact('tag'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        $tag = Tag::find($id);
        $tag->update($request->all());
        Tag::clear();
        return redirect(route('shop.tag.index'))->with('success', '编辑分类成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (Tag::check_children($id)) {
            return back()->with('error', '此分类下有分类，不能删除~');
        }

        Tag::destroy($id);
        Tag::clear();
        return back()->with('success', '删除分类成功~');
    }

    /**
     * ajax 排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $tag = Tag::find($request->id);
        $tag->sort_order = $request->sort_order;
        $tag->save();
        Tag::clear();
    }


    /**
     * ajax改变属性
     * @param Request $request
     */
    public function change_attr(Request $request)
    {
        $tag = Tag::find($request->id);
        $attr = $request->attr;
        $tag->$attr = !$tag->$attr;
        $tag->save();
        Tag::clear();
    }
}
